package org.cttic.visualization.visualization.util.routeIdentificationQuery;

import org.cttic.visualization.visualization.common.RouteIdentificationData;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.cttic.visualization.visualization.common.trackQueryData;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.chargingUtil;
import org.cttic.visualization.visualization.util.itineraryQuery.queryUtil;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-26 14:16
 */
@Component
public class routeQueryUtil {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    private  chargingUtil chargingUtil;
    @Autowired
    private queryUtil query;
    public void routeQuery(String provinceId,String vehPlate,String vehPlateColor,String queryValue1,
                           List<RouteIdentificationData.Identification> list,RouteIdentificationData rtData,String feeFlag){
        System.out.println("********识别点查询按行程编号开始查询*********");

        SearchHit[] hits = query.queryByTravelId(vehPlate, vehPlateColor, queryValue1,provinceId);
        if(hits.length>0) {
            System.out.println("********识别点查询完成行程开始查询*********");
            rtData.setIsOnline("1");
            route(provinceId,vehPlate, vehPlateColor,hits,queryValue1,list,rtData,feeFlag);
            System.out.println("********识别点查询完成行程结束查询*********");
        }else {
            SearchHit[] hits2 = query.queryByTravelId2(vehPlate, vehPlateColor, queryValue1,provinceId);
            if(hits2.length>0){
                System.out.println("********识别点查询未完成行程开始查询*********");
                rtData.setIsOnline("0");
                route(provinceId,vehPlate, vehPlateColor,hits2,queryValue1,list,rtData,feeFlag);
                System.out.println("********识别点查询未完成行程结束查询*********");
            }
        }

    }
    public void route(String provinceId,String vehPlate,String vehPlateColor,SearchHit[] hits,String queryValue1,
                      List<RouteIdentificationData.Identification> list,RouteIdentificationData rtData,String feeFlag){
        double totalReceivableAmt=0.0d;
        double totalDiscountAmt=0.0d;
        double totalBillAmt=0.0d;
        if(hits.length>0){
            for (SearchHit hit : hits) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                RouteIdentificationData.Identification identification = new RouteIdentificationData.Identification();
                Object tansTime = sourceAsMap.get("tansTime");
                if(tansTime!=null){
                    identification.setTransTime(DateUtil.timeToT(String.valueOf(tansTime)));
                }
                identification.setEndLat(queryValue1);
                identification.setVehStatus("0");
                identification.setProvinceId(provinceId);
                identification.setProvince(provinceData.provinceID.get(provinceId));
                //2-门架 1-收费站
                Object gantryType = sourceAsMap.get("gantryType");
                if(gantryType!=null){
                    if(gantryType.toString().equals("1")){
                        identification.setPointType("3");
                    }else {
                        identification.setPointType("1");
                    }
                }
                Object gantryId = sourceAsMap.get("gantryId");
                if(gantryId!=null){
                    identification.setGantryId(String.valueOf(gantryId));
                    if(String.valueOf(gantryId).length()>15){
                        identification.setDirection(String.valueOf(gantryId).substring(14, 15));
                    }
                }
                Object pointName = sourceAsMap.get("pointName");
                if(pointName!=null){
                    identification.setGantryName(String.valueOf(pointName));
                }
                if(identification.getPointType().equals("3")){
                    System.out.println("************"+DateUtil.TtoTime(identification.getTransTime())+"*************");
                    System.out.println("************"+hits[0].getSourceAsMap().get("tansTime").toString()+"*************");
                    if(DateUtil.TtoTime(identification.getTransTime()).compareTo(hits[0].getSourceAsMap().get("tansTime").toString())<=0){
                        //1-入口  2-出口
                        identification.setEnExFlag("1");
                    }else {
                        identification.setEnExFlag("2");
                    }
                }
                Map<String,Double> point = (Map<String,Double>)sourceAsMap.get("point");
                if(point!=null){
                    Double lat = point.get("lat");
                    Double lon = point.get("lon");
                    identification.setLat(lat+"");
                    identification.setLng(lon+"");
                }
                Object confidenceLevel = sourceAsMap.get("confidenceLevel");
                if(confidenceLevel!=null){
                    if(String.valueOf(confidenceLevel).equals("1")||String.valueOf(confidenceLevel).equals("2")){
                        identification.setSupport("1");
                    }else if(String.valueOf(confidenceLevel).equals("4")){
                        identification.setSupport("3");
                    }else if(String.valueOf(confidenceLevel).equals("5")){
                        identification.setSupport("2");
                    }else {
                        identification.setSupport("4");
                    }
                }
                if(gantryId!=null&&gantryType!=null) {
                    int pointType = Integer.parseInt(String.valueOf(gantryType));
                    //门架 省界门架
                    if (pointType == 2) {
                        System.out.println("****************查询mysql门架信息******************");
                        String sql = "SELECT gantryId,roadSectionId,roadSectionName,pileNumber,tollIntervals FROM `T_GANTRY_MOUNTING` " +
                                "where  gantryId= '" + gantryId + "'";
//                        List<mysqlUtilData> mysqlData = jdbcTemplate.query(sql, new BeanPropertyRowMapper(T_mysqlData.class));
                        List<Map<String, Object>> maps1 = jdbcTemplate.queryForList(sql);
                        if (maps1 != null && maps1.size() > 0) {
                            Map<String, Object> stringObjectMap = maps1.get(0);
                            String roadSectionId = stringObjectMap.get("roadSectionId").toString();
                            String roadSectionName = stringObjectMap.get("roadSectionName").toString();
                            String pileNumber = stringObjectMap.get("pileNumber").toString();
                            String tollIntervals = stringObjectMap.get("tollIntervals").toString();
                            identification.setSectionId(roadSectionId);
                            identification.setSectionCode(roadSectionId);
                            identification.setSectionName(roadSectionName);
                            identification.setPileNum(pileNumber);
                            identification.setTollIntervals(tollIntervals);
                            if (!tollIntervals.contains("|")) {
                                String sql2 = "SELECT unitId,provinceId,unitLength FROM `T_CHARGING_UNIT` where  unitId= '" + tollIntervals + "'";
                                List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql2);
                                if (maps != null && maps.size() > 0) {
                                    Map<String, Object> map = maps.get(0);
                                    Object unitLength = map.get("unitLength");
                                    double value = Double.parseDouble(String.valueOf(unitLength)) / 1000;//单位m 需转km
                                    System.out.println("****************查询mysql里程数信息******************");
                                    identification.setBillMile(value + "");
                                    if(map.get("provinceId")!=null) {
                                        String provinceId1 = map.get("provinceId").toString();
                                        identification.setProvinceId(provinceId1);
                                        identification.setProvince(provinceData.provinceID.get(Integer.parseInt(provinceId1)));
                                    }
                                }
                            } else {
                                Double BillAm = 0.00D;
                                String[] split = tollIntervals.split("\\|");
                                for (String s : split) {
                                    String sql3 = "SELECT unitId,provinceId,unitLength FROM `T_CHARGING_UNIT` where  unitId= '" + s + "'";
                                    List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql3);
                                    if (maps != null && maps.size() > 0) {
                                        Map<String, Object> map = maps.get(0);
                                        Object unitLength = map.get("unitLength");
                                        BillAm += Double.parseDouble(String.valueOf(unitLength)) / 1000;//单位m 需转km
                                        System.out.println("****************查询mysql里程信息******************");
                                        if(map.get("provinceId")!=null) {
                                            String provinceId1 = map.get("provinceId").toString();
                                            identification.setProvinceId(provinceId1);
                                            identification.setProvince(provinceData.provinceID.get(Integer.parseInt(provinceId1)));
                                        }
                                    }
                                }
                                identification.setBillMile(BillAm + "");
                            }
                        }
                    }else if (pointType == 1) {
                        String sql = "SELECT stationId,provinceId,stakeNum FROM `T_STATION` " +
                                "where  stationId= '" +
                                gantryId + "'";
                        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
                        if (maps != null && maps.size() > 0) {
                            Map<String, Object> stringObjectMap = maps.get(0);
                            if(stringObjectMap.get("stakeNum")!=null){
                                identification.setPileNum(stringObjectMap.get("stakeNum").toString());
                            }
                            System.out.println("****************收费站查询mysql信息******************");
                            if(stringObjectMap.get("provinceId")!=null){
                                String provinceId1 = stringObjectMap.get("provinceId").toString();
                                identification.setProvinceId(provinceId1);
                                identification.setProvince(provinceData.provinceID.get(Integer.parseInt(provinceId1)));
                            }
                        }
                    }
                }
                if(identification.getBillMile()!=null&&feeFlag!=null&&feeFlag.equals("1")){

                    String queryBillAm = chargingUtil.queryBillAm(vehPlate, vehPlateColor, provinceId, identification.getSectionCode(), identification.getDirection(), identification.getBillMile(), identification.getTransTime(), identification.getTollIntervals());
                    System.out.println("********"+queryBillAm+"*******");
                    if(queryBillAm!=null&&queryBillAm.contains("|")){
                        String[] split = queryBillAm.split("\\|");
                        if(split.length>=3){
                            String bill = split[0];
                            identification.setBillAmount(bill);
                            String receivable = split[1];
                            identification.setReceivableAmount(receivable);
                            String discount = split[2];
                            identification.setDiscountAmount(discount);
                            totalReceivableAmt+=Double.parseDouble(receivable);
                            totalBillAmt+=Double.parseDouble(bill);
                            totalDiscountAmt+=Double.parseDouble(discount);
                            System.out.println("receivable:"+Double.parseDouble(receivable));
                            System.out.println("bill:"+Double.parseDouble(bill));
                            System.out.println("discount:"+Double.parseDouble(discount));
                            System.out.println("totalReceivableAmt:"+totalReceivableAmt);
                            System.out.println("totalBillAmt:"+totalBillAmt);
                            System.out.println("totalDiscountAmt:"+totalDiscountAmt);

                        }
                    }
                }
                list.add(identification);
//                if(identification.getReceivableAmount()!=null){
//                    totalReceivableAmt+=Double.parseDouble(identification.getReceivableAmount());
//                }
//                if(identification.getDiscountAmount()!=null){
//                    totalDiscountAmt+=Double.parseDouble(identification.getDiscountAmount());
//                }
//                if(identification.getBillAmount()!=null){
//                    totalBillAmt+=Double.parseDouble(identification.getBillAmount());
//                }
            }
            rtData.setTotalArAmout(totalBillAmt+"");
            rtData.setTotalDiscountAmt(totalDiscountAmt+"");
            rtData.setTotalBillAmt(totalBillAmt+"");
            rtData.setTotalReceivableAmt(totalReceivableAmt+"");
        }
    }
}
